import {
    createRouter as _createRouter,
    createWebHashHistory,
} from 'vue-router'

import adminLayout from '~/layouts/Admin.vue'

const routes = [
    {
        path: "/",
        name: "admin",
        component: adminLayout
    },
    {
        path: "/login",
        name: "/login",
        component: () =>
            import("~/views/Login.vue"),
        meta: {
            title: "登录页"
        }
    },
    {
        path: '/:pathMatch(.*)*',
        name: '404',
        component: () =>
            import ("~/views/404.vue")
    },
]

const asyncRoutes = [{
    path: "/",
    component: () => 
        import ("~/views/Index.vue")
    ,
    meta: {
        title: "后台首页"
    }
},{
    path: "/goods/list",
    component: () => 
        import ("~/views/goods/List.vue")
    ,
    meta: {
        title: "商品管理"
    }
},{
    path: "/category/list",
    component: () => 
        import ("~/views/category/List.vue")
    ,
    meta: {
        title: "分类管理"
    }
},{
    path: "/skus/list",
    component: () =>
        import ("~/views/skus/List.vue"),
    meta: {
        title: "规格管理"
    }
},{
    path: "/comment/list",
    component: () =>
        import ("~/views/comment/List.vue"),
    meta: {
        title: "评论管理"
    }
},
{
    path: "/user/list",
    component: () =>
        import ("~/views/user/List.vue"),
    meta: {
        title: "用户管理"
    }
},
{
    path: "/level/list",
    component: () =>
        import ("~/views/level/List.vue"),
    meta: {
        title: "会员等级"
    }
},
{
    path: "/coupon/list",
    component: () =>
        import ("~/views/coupon/List.vue"),
    meta: {
        title: "优惠券管理"
    }
},
{
    path: "/order/list",
    component: () =>
        import ("~/views/order/List.vue"),
    meta: {
        title: "订单管理"
    }
},
{
    path: "/setting/base",
    component: () =>
        import ("~/views/setting/Base.vue"),
    meta: {
        title: "基础设置"
    }
},
{
    path: "/setting/buy",
    component: () =>
        import ("~/views/setting/Buy.vue"),
    meta: {
        title: "支付设置"
    }
},
{
    path: "/notice/list",
    component: () =>
        import ("~/views/notice/List.vue"),
    meta: {
        title: "公告管理"
    }
},
{
    path: "/setting/ship",
    component: () =>
        import ("~/views/setting/Ship.vue"),
    meta: {
        title: "物流设置"
    }
},
{
    path: "/image/list",
    component: () =>
        import ("~/views/image/List.vue"),
    meta: {
        title: "图库管理"
    }
},
{
    path: "/manager/list",
    component: () =>
        import ("~/views/manager/List.vue"),
    meta: {
        title: "管理员管理"
    }
},
{
    path: "/role/list",
    component: () =>
        import ("~/views/role/List.vue"),
    meta: {
        title: "角色管理"
    }
},
{
    path: "/access/list",
    component: () =>
        import ("~/views/access/List.vue"),
    meta: {
        title: "权限管理"
    }
},
{
    path: "/distribution/index",
    component: () =>
        import ("~/views/distribution/Index.vue"),
    meta: {
        title: "分销员管理"
    }
},
{
    path: "/distribution/setting",
    component: () =>
        import ("~/views/distribution/Setting.vue"),
    meta: {
        title: "分销设置"
    }
}
]

function createRouter() {
    return _createRouter({
        history: createWebHashHistory(),
        routes
    })
}

export const router = createRouter()

export function addRoutes(menus) {
    let hasNewRoutes = false
    const findAndaddRoutesByMenus = (arr) => {
        arr.forEach(e => {
            let item = asyncRoutes.find(o => o.path == e.frontpath)
            if (item && !router.hasRoute(item.path)) {
                router.addRoute("admin", item)
                hasNewRoutes = true
            }
            if (e.child && e.child.length > 0) {
                findAndaddRoutesByMenus(e.child)
            }
        })
    }

    findAndaddRoutesByMenus(menus)
    return hasNewRoutes
}